<?php
//*********************************** 
//Universal Media Publishing Platform
//Login functions
//*********************************** 

if (isset($_COOKIE['login']) && isset($_COOKIE['pass'])) {
    $login = $_COOKIE['login'];
    $pass = $_COOKIE['pass'];
} else {
    $login="";
    $pass="";
}



if (check($login, $pass)){
    $form1 = "hide";
    $form2 = "show";
}  
else {
    $form1 = "show";
    $form2 = "hide";
}

if (isset($_POST['log'])) { 
    $login = $_POST['onlog'];
    $pass = md5($_POST['sapa']);
    if (check($login, $pass)) {
        setcookie("login", $login);
        setcookie("pass", $pass);
        setcookie("logged", true);
        $autherr = "";
        header('Location: '.$_SERVER['HTTP_REFERER']);
    } else $autherr = "Неверные данные";
}

if (isset($_POST['logout'])) {
   setcookie("login", "");
   setcookie("pass", "");
   setcookie("logged", false);
   header('Location: '.$_SERVER['HTTP_REFERER']);
}
  
function check($login, $pass) {
    $query = "SELECT * FROM users WHERE login='$login'";
    $res = mysql_query($query);
    if (mysql_num_rows($res)!=0){
        while($row = mysql_fetch_array($res)){
            $user = $row;
        }
        
        global $usrgroup, $name;
        $name = $user["name"];
        $usrgroup = $user["group"];
    
        if (($login == $user["login"]) && ($pass == $user["pass"]) && (isset($user))) return true;
        else return false;
    } else return false;
}
?>
